#include "archiveqbytearraystream.h"

ArchiveQByteArrayStream::ArchiveQByteArrayStream()
{
    ba = new QByteArray;
    pos = 0;
}

ArchiveQByteArrayStream::~ArchiveQByteArrayStream()
{
    // do not delete the byte array, QCache will do that
}

QByteArray *ArchiveQByteArrayStream::byteArray()
{
    return ba;
}

int ArchiveQByteArrayStream::Write(const void *data, unsigned int size, unsigned int *processedSize)
{
    ba->replace(pos, size, static_cast<const char *>(data), size);
    pos += size;

    if (processedSize) *processedSize = size;
    return 0;
}

int ArchiveQByteArrayStream::Seek(long long offset, unsigned int seekOrigin, unsigned long long *newPosition)
{
    if (seekOrigin == SEEK_SET) pos = offset;
    if (seekOrigin == SEEK_CUR) pos += offset;
    if (seekOrigin == SEEK_END) pos = ba->size() + offset;

    if (newPosition) *newPosition = pos;
    return 0;
}

int ArchiveQByteArrayStream::SetSize(unsigned long long size)
{
    ba->resize(size);
    return 0;
}
